package com.tgsoft.security.service;

import cn.hutool.crypto.SmUtil;
import com.tgsoft.security.user.UserDetail;
import lombok.AllArgsConstructor;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 账号登录 UserDetailsService
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
//    private final SysUserDetailsService sysUserDetailsService;
//    private final SysUserDao sysUserDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<UserDetail> ulist = new ArrayList<>() ;

        UserDetail u11 = UserDetail.builder()
            .id(10025L).email("")
            .gender(1)
            .orgId(12321L)
            .password(SmUtil.sm3("123456"))
            .username("PDA")
            .isEnabled(true)
            .isAccountNonExpired(true)
            .isAccountNonLocked(true)
            .isCredentialsNonExpired(true)
            .build();
        Set<String> authoritySet=new HashSet<>();
        authoritySet.add("ROLE_USER");
        authoritySet.add("ROLE_ADMIN");
        u11.setAuthoritySet(authoritySet);
        ulist.add(u11);



        ulist.add(UserDetail.builder()
            .id(10026L).email("")
            .gender(1)
            .orgId(12321L)
            .password("123456")
            .username("fhong").build());

        ulist.add(UserDetail.builder()
            .id(10027L).email("")
            .gender(1)
            .orgId(12321L)
            .password("123456")
            .username("LDW").build());


        var u1 = ulist.stream().filter(u -> u.getUsername().equals(username)).findFirst();

        var u2=u1.get();

        if (u2==null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        return u2;
    }

}
